package com.lzy.crm.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.util.BootStrapReturn;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.domain.Notice;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.domain.ProfitRule;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.NoticeService;
import com.lzy.crm.service.ProfitRuleService;
import com.lzy.crm.util.BootJsonResponse;
import com.lzy.crm.util.UserConstants;


/**
 * 消息控制器
 * @author chenshiping
 *
 */
@Controller
@RequestMapping("/admin")
public class NoticeController {
	
	Logger logger= Logger.getLogger(NoticeController.class);
	
	@Resource
	NoticeService noticeService;
	
	@Resource
	ProfitRuleService profitRuleService;
	
	@RequestMapping("/notice")  
	public String notice(HttpServletRequest request,ModelMap model) throws Exception{
		System.out.println("公告页面");
		String id= request.getParameter("id");
		Notice notice=noticeService.getNoticeById(id);
		model.put("notice", notice);
		return "notice/notice";
	}
	
	/**
	 * 超管权限
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/notice_list")  
	public String noticeList(HttpServletRequest request,ModelMap model) throws Exception{
		User user = (User)request.getSession().getAttribute("userSession");
		if(StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
			return "notice/notice_list";
		}
		return "notice/notice";
	}
	
	
	@RequestMapping(path="/notice_list_data",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String noticeListData(HttpServletRequest request, @RequestParam Map<String, String> form,
			ModelMap model) {
		BootStrapReturn<Notice> result = new BootStrapReturn<Notice>();
		try {
			String title = request.getParameter("title");
			User user = (User)request.getSession().getAttribute("userSession");
			if(!StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
				return null;
			}
			Integer currentPage = Integer.parseInt(request.getParameter("numPerPage"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageNum"));
			PageQuery pageQuery = new PageQuery(numPerPage,currentPage);
			pageQuery.setIsQueryTotal(true); 
			List<Notice> noticeList =noticeService.getCardFlowList(title,pageQuery);
			result.setPage(pageQuery.getPageIndex());
			result.setTotal(pageQuery.getTotalCount());
			result.setRows(noticeList);
		} catch (Exception e) {
			logger.error("消息列表", e);
		}
		return JsonUtil.toJson(result) ;
	}
	
	
	
	@RequestMapping(path="/notice_edit",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String noticeEdit(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String id= request.getParameter("id");
			String status = request.getParameter("status");
			String title= request.getParameter("title");
			String contents = request.getParameter("contents");
			User user = (User)request.getSession().getAttribute("userSession");
			if(StringUtil.isNotEmpty(id)){
				Notice notice=noticeService.getNoticeById(id);
				if(StringUtil.isNotEmpty(status)){
					notice.setStatus(status);
				}
				if(StringUtil.isNotEmpty(title)){
					notice.setTitle(title);
				}
				if(StringUtil.isNotEmpty(contents)){
					notice.setContents(contents);
				}
				noticeService.updateByPrimaryKeySelective(notice);
			}else{
				Notice notice=new Notice();
				notice.setTitle(title);
				notice.setContents(contents);
				notice.setCreateTime(new Date());
				notice.setStatus("0");
				notice.setCreateUserId(user.getId());
				noticeService.insertSelective(notice);
			}
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个公告失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	@RequestMapping("/notice_add")  
	public String notice_add(HttpServletRequest request,ModelMap model) throws Exception{
		String id =request.getParameter("id");
		if(StringUtil.isNotEmpty(id)&&!StringUtil.equals(id, "0")){
			Notice notice=noticeService.getNoticeById(id);
			model.put("notice", notice);
		}
		return "notice/notice_edit";
	}
	
	/******** 手机端  ***********/
	
	@RequestMapping("/notice_list_mobile")  
	public String notice_list(HttpServletRequest request,ModelMap model) throws Exception{
		String id= request.getParameter("id");
		Notice notice=noticeService.getNoticeById(id);
		model.put("notice", notice);
		return "mobile/notice/notice_list";
	}
	
	
	@RequestMapping("/notice_edit_mobile")  
	public String noticeEditMobile(HttpServletRequest request,ModelMap model) throws Exception{
		String id= request.getParameter("id");
		if(StringUtil.isNotEmpty(id)){
			Notice notice=noticeService.getNoticeById(id);
			model.put("notice", notice);
		}
		return "mobile/notice/notice_edit";
	}

	
	
	
	@RequestMapping("/notice_mobile")  
	public String noticeMobile(HttpServletRequest request,ModelMap model) throws Exception{
		String id= request.getParameter("id");
		Notice notice=noticeService.getNoticeById(id);
		model.put("notice", notice);
		return "mobile/notice/notice";
	}

	@RequestMapping("/notice_profitRule_mobile")  
	public String noticeProfitRuleMobile(HttpServletRequest request,ModelMap model) throws Exception{
		PpUser user = (PpUser)request.getSession().getAttribute("userSession");
		ProfitRule profitRule = profitRuleService.getById(user.getUserLevel());
		model.put("profitRule", profitRule);
		return "mobile/notice/notice_profit";
	}
	
	
}
